<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentChangePicture.class.php, $Author:  $ */



/** Change profile picture component
 *
 * Parameters:
 *  1. oUser (mandatory) - user account object (ItemAccount)
 *
 * @package module.account
 * @version $Id: PhpFox_ComponentChangePicture.class.php 349 2008-02-18 17:45:50Z phpfoxsv $
 *
 */
class PhpFox_Mod_Account_ComponentChangePicture extends BaseComponent
{

    /** Processing method
     */
	function process()
	{
	    $oAcc = $this->getParam('oUser');
	    /* @var $oAcc PhpFox_Mod_Account_ItemAccount */
	    assert($oAcc != null);
	    
	    $oReq = &App::getRequest();
	    //processing
        switch($oReq->getAction())
        {
            case 'change_picture':
                $oImage = &App::newObject('file_Image');
                /* @var $oImage file_Image */

                //overide max image size limit
                $oImage->aLimits['MAX_IMAGE_W']   = App::getSetting('upload_set_size_user');
                $oImage->aLimits['MAX_IMAGE_H']   = App::getSetting('upload_set_size_user');
                $oImage->aLimits['FILE_MAX_SIZE'] = App::getSetting('max_upload_size') * 1024;

                $sPicPath = App::getParam('user.pic.path');
                if ($oImage->upload('image', $sPicPath, $oAcc->aData['user'], 'jpg'))
                {
                	$oImage->loadInfo($sPicPath.$oAcc->aData['user'].'.jpg');
                    //make thumbnails
                    foreach(App::getParam('user.pic.sizes') as $iSize)
                    {
                        $oImage->makeThumbnail($sPicPath.$oAcc->aData['user'].'_'.$iSize.'.jpg', $iSize, $iSize);
                    }
                    
	                if(!App::getParam('bSaveOriginalImages'))
	                {
	                	unlink($sPicPath . $oAcc->aData['user'].'.jpg');
	                }
	                
                    $oUrl = App::getUrl();
                    $oUrl->setParam('done', 1);   
                    
                    App::forward($oUrl->getUrl());
                }
                else
                {
                    $this->addErrors($oImage->getErrors());
                }

                break;
        }


		$oTpl = &$this->_oModule->getTemplate();
		$oTpl->assign(array(
			'aErrors'    => $this->getErrors(),
			'bProcessed' => $oReq->getInt('done', false),
		));
		$srcHtml = $oTpl->fetch('ChangePicture.html');
		return $srcHtml;
	}

}
?>